package motor;

import java.awt.Color;
import java.awt.Graphics;



public class ModuleArme
{
	float rythme;
	float force;
	float portee;
	
	float attente = 0;
	
	Unite mere;
	
	Unite cible;
	
	private ModuleArme(Unite mere, float rythme, float force, float portee)
	{
		super();
		this.mere = mere;
		this.rythme = rythme;
		this.force = force;
		this.portee = portee;
	}

	static public ModuleArme getFusil( Unite u )
	{
		return new ModuleArme( u , 10, 10, 100 );
	}
	
	public void feu( Motor m , Unite cibleA )
	{
		this.cible = null;
		
		if( attente > 0 )
		{
			attente -= 1;
		}
		else if( m.s.cansee( mere.getPos() , cibleA.getPos() ) == null && mere.distance(cibleA.getPos() )  < portee )
		{
			this.cible = cibleA;
			
			this.cible.damage(force);
			
			attente = rythme;
			
			if( this.cible.isDead() )
			{
				this.cible = null;
			}
		}
	}

	public float getPortee()
	{
		return portee;
	}

	public void draw(Graphics buffer_canvas )
	{
		buffer_canvas.setColor( Color.red );
		buffer_canvas.drawOval((int)(mere.getPos().x-portee), (int)(mere.getPos().y-portee), (int)portee * 2, (int)portee * 2);
		
		
		if( cible != null )
		{
			buffer_canvas.setColor( Color.yellow );
			buffer_canvas.drawLine( (int)mere.getPos().x , (int)mere.getPos().y , (int)cible.getPos().x , (int)cible.getPos().y );
		}
	}
}
